import http from '@/common/request'
import store from '../store/index';

// 上传
export function upload(imgList = [], key = 'file') {
	let successUp = 0; //成功
	let failUp = 0; //失败
	let length = imgList.length; //总数
	let count = 0; //第几张
	let successList = [];
	return new Promise((resolve, reject) => {
		(function uploadOneByOne() {
			uni.uploadFile({
				url: `${http.BASE_URL}/sys/files/upload`,
				filePath: imgList[count],
				header: {
					'Authorization': 'Bearer ' + store.getters.token
				},
				name: key,
				success(res) {
					let result = JSON.parse(res.data);
					console.log(result)
					if (result.code == 200) {
						successUp++; //成功+1
						successList.push(result.data.id);
					} else {
						failUp++; //失败+1
					}
				},
				fail: function(e) {
					failUp++; //失败+1
				},
				complete(res) {
					count++; //下一张
					if (count >= length) {
						resolve(successList);
						return;
					} else {
						//递归调用，上传下一张
						uploadOneByOne();
					}
				},
			});
		})();
	});
}

export function getDicts(dictType) {
  return http.request({
    url: '/system/dict/data/type/' + dictType,
    method: 'get'
  })
}

// 回显数据字典
export function selectDictLabel(datas, value) {
	var actions = [];
	Object.keys(datas).some((key) => {
		if (datas[key].dictValue == ('' + value)) {
			actions.push(datas[key].dictLabel);
			return true;
		}
	})
	return actions.join('');
}

// 根据参数键名查询参数值
export function getConfigKey(configKey) {
  return http.request({
    url: '/system/config/configKey/' + configKey,
    method: 'get'
  })
}


// 查询数据
export function queryData(query) {
  return http.request({
    url: '/sys/dataset/query',
    method: 'post',
    data: query
  })
}

// 查询收件箱消息列表
export function inMsg(query) {
  return http.request({
    url: '/sys/msg/in',
    method: 'get',
    params: query,
	needToken: true
  })
}

// 查询消息详细
export function getMsg(id) {
  return http.request({
    url: '/sys/msg/' + id,
    method: 'get',
	needToken: true
  })
}

// 新增消息
export function addMsg(data) {
  return http.request({
    url: '/sys/msg',
    method: 'post',
    data: data,
	needToken: true
  })
}

// 修改消息
export function updateMsg(data) {
  return http.request({
    url: '/sys/msg',
    method: 'put',
    data: data,
	needToken: true
  })
}

// 删除消息
export function delMsg(id) {
  return http.request({
    url: '/sys/msg/' + id,
    method: 'delete',
	needToken: true
  })
}

// 标记消息已读
export function readMsg(id) {
  return http.request({
    url: '/sys/msg/read/'+id,
    method: 'get',
	needToken: true
  })
}

// 最新公告
export function newNotice() {
	return http.request({
		url: '/sys/public/notice',
		method: 'get',
		needToken: true
	})
}